﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;
using System.IO;

namespace TorchView.PakViewer
{
    public class DataViewModel
    {
        private DataBlock _block;
        private DataProperty _property;
        private bool isBlock;
        private List<DataViewModel> _subblocks = new List<DataViewModel>();

        public DataViewModel(Stream dataStream)
        {
            _block = AdmSerializer.Load(dataStream);
            isBlock = true;
            createSubBlocks();
        }

        public DataViewModel(DataBlock block)
        {
            _block = block;
            isBlock = true;
            createSubBlocks();
        }

        public DataViewModel(DataProperty property)
        {
            _property = property;
            isBlock = false;
        }

        public IEnumerable<DataViewModel> SubBlocks
        {
            get
            {
                return _subblocks;
            }
        }

        private void createSubBlocks()
        {
            if(isBlock)
            {
                foreach (DataBlock subblock in _block.SubBlocks)
                {
                    _subblocks.Add(new DataViewModel(subblock));
                }

                foreach (DataProperty property in _block.Properties)
                {
                    _subblocks.Add(new DataViewModel(property));
                }
            }
        }

        public string Name
        {
            get
            {
                if (isBlock)
                    return _block.ToString();
                else
                    return _property.ToString();
            }
        }
    }
}
